<?php

namespace app\admin\controller;

use app\admin\logic\SpaceLogic;

/**
 * 空间信息api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/16
 * Time: 09:48
 */
class Space extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->s_logic = new SpaceLogic();
    }

    /**
     * 获取分部统计
     * @return \think\response\Json
     */
    public function ajax_get_branch_total()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->s_logic->get_search_branch_total_maps($params['branchid']);

        $b_list = model('branch')->get_list($maps, 'id,name');
        $list = [];
        foreach ($b_list as $k => $v) {
            $list[$k]['branch_zw'] = $v['name'];

            //工位统计
            $r = $this->s_logic->total_ws($v['id'], -1, -1, 1);
            $list[$k]['gw_info'] = $r['data'];

            //办公室统计
            $r = $this->s_logic->total_ws($v['id'], -1, -1, 2);
            $list[$k]['bgs_info'] = $r['data'];

            //工位+办公室
            $r = $this->s_logic->total_ws($v['id'], -1, -1, -1);
            $list[$k]['total_info'] = $r['data'];
        }

        $data['list'] = $list;
        return json($this->s_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取楼层统计
     * @return \think\response\Json
     */
    public function ajax_get_floor_total()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->s_logic->get_search_floor_total_maps($params['branchid'], $params['floorid']);

        $f_list = model('floor')->get_list($maps, 'id,branchid,name');
        $list = [];
        foreach ($f_list as $k => $v) {
            $b_maps = [];
            $b_maps[] = ['id', '=', $v['branchid']];
            $b_detail = model('branch')->get_detail($b_maps, 'id,name');
            $list[$k]['floor_zw'] = $b_detail['name'] . '-' . $v['name'];

            //工位统计
            $r = $this->s_logic->total_ws($b_detail['id'], $v['id'], -1, 1);
            $list[$k]['gw_info'] = $r['data'];

            //办公室统计
            $r = $this->s_logic->total_ws($b_detail['id'], $v['id'], -1, 2);
            $list[$k]['bgs_info'] = $r['data'];

            //工位+办公室
            $r = $this->s_logic->total_ws($b_detail['id'], $v['id'], -1, -1);
            $list[$k]['total_info'] = $r['data'];
        }

        $data['list'] = $list;
        return json($this->s_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取可视化概览信息
     * @return \think\response\Json
     */
    public function ajax_get_overview()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $r = $this->s_logic->total_ws($params['branchid'], $params['floorid'], $params['use_status'], $params['work_type']);
        $overview = $r['data'];

        $data['total_num'] = $overview['total_num'];
        $data['vacant_ws_num'] = $overview['vacant_ws_num'];
        $data['person_num'] = $overview['person_num'];
        $data['use_ws_num'] = $overview['use_ws_num'];
        $data['use_rate'] = $overview['use_rate'];
        return json($this->s_logic->get_response(1, 'OK', $data));
    }

    /**
     * 获取工位
     * @return \think\response\Json
     */
    public function ajax_get_ws()
    {
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $now_date = date('Y-m-d');
        $s = new \app\common\controller\Space();

        $maps = $this->s_logic->get_search_ws_maps($params['branchid'], $params['floorid'], $params['use_status'], $params['work_type']);
        //$limit = ($params['page'] - 1) * $params['rows'];

        $ws_list = model('work_station')->get_list($maps, 'id,regionid,number,work_type,price,company_name,company_out_time', 'id desc');
        $list = [];
        foreach ($ws_list as $k => $v) {
            $ws_list[$k]['work_type_zw'] = $v->work_type_name;
            $ws_list[$k]['price'] = deal_number_div_100($v['price']);

            //查找区域
            $r_maps = [];
            $r_maps[] = ['id', '=', $v['regionid']];
            $r_detail = model('region')->get_detail($r_maps, 'id,floorid,name');
            $ws_list[$k]['region_zw'] = $r_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id', '=', $r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps, 'id,branchid,name');
            $ws_list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id', '=', $f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps, 'id,name');
            $ws_list[$k]['branch_zw'] = $b_detail['name'];

            //获取空间工位信息
            $cp_ws_info = $s->get_space_ws_info($v['regionid'], $v['id'], $v['company_out_time']);
            $ws_list[$k]['use_status'] = $cp_ws_info['use_status'];
            $ws_list[$k]['use_status_zw'] = $cp_ws_info['use_status_zw'];
            $ws_list[$k]['use_rate'] = $cp_ws_info['use_rate'];
            $ws_list[$k]['person_num'] = $cp_ws_info['person_num'];
            $ws_list[$k]['is_holiday_work'] = $cp_ws_info['is_holiday_work'];
            $ws_list[$k]['is_evening_work'] = $cp_ws_info['is_evening_work'];
            $ws_list[$k]['ccids'] = $cp_ws_info['ccids'];
            $ws_list[$k]['remark'] = $cp_ws_info['remark'];
            $ws_list[$k]['status'] = $cp_ws_info['status'];
            $ws_list[$k]['status_zw'] = $cp_ws_info['status_zw'];

            //计算剩余天数
            $ws_list[$k]['rest_day'] = get_time_diff_day($v['company_out_time'], $now_date);

            //计算空置天数
            $ws_list[$k]['vacant_day'] = get_time_diff_day($now_date, $v['company_out_time']);

            $list[$k] = $v->toArray();
        }

        $data['list'] = $list;
        return json($this->s_logic->get_response(1, 'OK', $data));
    }
}
